package com.cjm.one.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.model.system.entity.SysDept;
import com.cjm.one.model.system.vo.SysDeptAllVo;
import com.cjm.one.model.system.vo.SysResourceVo;
import com.cjm.one.system.mapper.SysResourceMapper;
import com.cjm.one.system.service.SysResourceService;
import com.cjm.one.model.system.entity.SysResource;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 29289
* @description 针对表【sys_resource(资源表)】的数据库操作Service实现
* @createDate 2024-09-05 16:21:14
*/
@Service
@Slf4j
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource>
    implements SysResourceService {
    @Resource
    private SysResourceMapper sysResourceMapper;

    @Override
    public List<SysResourceVo> selectListPage(SysResource sysResource) {
        LambdaQueryWrapper<SysResource> sysResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(sysResource.getTitle())){
            sysResourceLambdaQueryWrapper.eq(SysResource::getTitle, sysResource.getTitle());
        }


        List<SysResource> sysResources = sysResourceMapper.selectList(sysResourceLambdaQueryWrapper);
        List<SysResourceVo> sysResourceVos = treeMenu(sysResources, 0L);
        return sysResourceVos;
    }

    @Override
    public Boolean removeByIdSysResource(Long id) {
        LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysResource::getParentId, id);
        SysResource sysResource = sysResourceMapper.selectOne(queryWrapper);
        if (sysResource != null) {
            throw  new GlobalException(ResultCodeEnum.DELETE_MENU_ERROR);
        } else {
            int i = sysResourceMapper.deleteById(id);
            return i > 0;
        }

    }
    /**
     * @description: 递归生成菜单树
     * @author: cjm
     * @date: 2024/11/21 13:35
     * @param: [list, parentId]
     * @return: java.util.List<com.cjm.one.model.system.vo.SysResourceVo>
     **/
    public List<SysResourceVo> treeMenu (List<SysResource> list, Long parentId) {
        if (list == null || list.size() == 0) {
            return null;
        }
        List<SysResourceVo> data = new ArrayList<>();
        Integer sort = 1;
        for (SysResource resource : list) {
            if (resource.getParentId().equals(parentId)) {
                sort++;
                SysResourceVo sysResourceVo = new SysResourceVo();
                BeanUtils.copyProperties(resource, sysResourceVo);
                sysResourceVo.setChildren(treeMenu(list, resource.getId()));
                log.info("排序后的数据parentId:{}", resource.getId());
                data.add(sysResourceVo);
            }
        }
        log.info("排序后的数据sort:{}", sort);
        return data;
    }
}




